package cn.wolfcode.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.service.*;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.ConsumptionStatusEnum;
import cn.wolfcode.util.PermissionAnnotation;
import cn.wolfcode.web.util.SessionUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private IConsumptionItemService consumptionItemService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @PermissionAnnotation(name="消费单页面",expression="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("business",businessService.listAll());

        model.addAttribute("PageInfo", pageInfo);
        model.addAttribute("status",ConsumptionStatusEnum.values());
        return "consumption/list";
    }


    @PermissionAnnotation(name="消费单删除",expression="consumption:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           consumptionService.delete(id);
        }
        return "redirect:/consumption/list";
    }


    @PermissionAnnotation(name="消费单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption,Integer settlement) {
        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            if (settlement!=null&&settlement==1){
                consumption.setStatus(settlement);
                consumption.setPayee(SessionUtil.getAtt(SessionUtil.USER));
                consumption.setPayTime(new Date());
                consumptionService.updateCheck(consumption);
            }
            if (settlement!=null&&settlement==2){
                consumption.setStatus(settlement);
                consumption.setAuditTime(new Date());
                consumption.setAuditor(SessionUtil.getAtt(SessionUtil.USER));
               consumptionService.updateReview(consumption);
            }
            if (settlement!=null&&settlement==3)
            {
                consumption.setStatus(settlement);
                consumption.setAuditTime(new Date());
                consumption.setAuditor(SessionUtil.getAtt(SessionUtil.USER));
                consumptionService.updateReview(consumption);
            }
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }


    @PermissionAnnotation(name="消费单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
       model.addAttribute("categoryItem",systemDictionaryItemService.selectBySn("business"));
       model.addAttribute("payTypeItem",systemDictionaryItemService.selectBySn("pay_type"));
        if (id != null) {
            Consumption consumption = consumptionService.get(id);

            model.addAttribute("consumption",consumption);

            model.addAttribute("consumptionItem",consumptionItemService.selectByCno(consumption.getCno()));
        }
        model.addAttribute("businesses",businessService.listAll());
        return "consumption/input";
    }

    @RequestMapping("save")
    public String save(Model model,Long id,Integer status){
        appointmentService.updateStatus(id,status);
        Appointment appointment = appointmentService.get(id);
        Consumption consumption = new Consumption();
        consumption.setAppointmentAno(appointment.getAno());
        String date = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        String s = RandomUtil.randomNumbers(5);
        consumption.setCno(date+s);
        consumption.setStatus(ConsumptionStatusEnum.CONSUME.getValue());
        consumption.setInfo(appointment.getInfo());
        consumption.setBusiness(appointment.getBusiness());
        consumption.setPayee(SessionUtil.getAtt(SessionUtil.USER));
        consumption.setCheckinTime(appointment.getCreateTime());
        consumption.setCustomerName(appointment.getContactName());
        consumption.setCustomerTel(appointment.getContactTel());
        Date createTime = new Date();
        consumption.setCreateTime(createTime);
        consumptionService.save(consumption);
        model.addAttribute("consumption",consumptionService.get(consumption.getId()));
        model.addAttribute("businesses",businessService.listAll());

        return "consumption/input";
    }
}
